import java.awt.Event;

import EventPackage.*;

public class HeartBeat implements Runnable {

	Thread runner;
	final long beatTime;
	EventPackage.Event hbEvent = null;

	// New Event manager
	EventManagerInterface hbManager = null;// Interface object to the heart

	public HeartBeat(EventManagerInterface manager, EventPackage.Event event, long timePeriod) throws Exception {
		hbManager = manager;
		beatTime = timePeriod;
		hbEvent = event;

		runner = new Thread(this, String.valueOf(hbManager.GetMyId()));
		System.out.println("Started Heatbeat");
		runner.start();
	}

	public void run() {
		// Display info about this particular thread
		while (true) {
			postHeartBeat();
			try {
				Thread.sleep(beatTime);
			} catch (InterruptedException e) {
				System.out.println(" interrupt exception");
			}
		}
	}

	public void postHeartBeat() {

		// Here we send the event to the event manager.

		try {
			hbManager.SendEvent(hbEvent);
			// System.out.println( "Sent heart beat Event" );

		} // try

		catch (Exception e) {
			System.out.println("Error Posting Temperature:: " + e);

		} // catch

	} // PostHeartBeat

	public static void startThread(EventManagerInterface manager, EventPackage.Event event,
			long timePeriod) throws Exception{
		HeartBeat heartBeat = new HeartBeat(manager, event, timePeriod);

	}

}
